/**
 * Created with IntelliJ IDEA.
 * User: weixintao
 * Date: 14-12-11
 * Time: 下午5:01
 * To change this template use File | Settings | File Templates.
 */
package Hxw3DEngine.core.component.animation {

import Hxw3DEngine.core.materials.shader.SkinnedShader;
import Hxw3DEngine.core.Resource.animation.SkeletonAnimationClip;
import Hxw3DEngine.core.Resource.ResourceSkeleton;

import flash.utils.Dictionary;

public class SkeletonClipCacheSet {

    private static var _cache_skeleton:Vector.<String> = new Vector.<String>();
    private static var _cache_animator:Vector.<String> = new Vector.<String>();
    private static var _enable:Boolean = true;
    private static var _instances:Dictionary = new Dictionary();
    //private static var _se:muiltiLatonSe = new muiltiLatonSe();

    public static var cache_size:uint = 0;
    public static var cache_clips:uint = 0;

    private var _caches:Dictionary;
    private var _assetNamespace:String;

    public static function  enableCache(assetNamespace:String,skeleton:ResourceSkeleton):void
    {
        if(_enable){
            for(var i:uint = 0;i<_cache_skeleton.length;i++){
                if(assetNamespace.search(_cache_skeleton[i])>=0){
                    var cacheSet:SkeletonClipCacheSet = new SkeletonClipCacheSet(null,_cache_skeleton[i]);
                    _instances[skeleton]=cacheSet;
                    trace("enableCache: "+_cache_skeleton[i]);
                    break;
                }
            }
        }
    }
    public static function cache_skeletons(skeletons:Vector.<String>):void
    {
        for(var i:uint = 0;i<skeletons.length;i++){
            _cache_skeleton.push(skeletons[i]);
        }
    }
    public static function cache_animators(clips:Vector.<String>):void
    {
        for(var i:uint = 0;i<clips.length;i++){
            _cache_animator.push(clips[i]);
        }
    }

    public static function getInstance(skeleton:ResourceSkeleton):SkeletonClipCacheSet
    {
        if(skeleton in _instances){
            return _instances[skeleton];
        }
         return null;
    }


    public function SkeletonClipCacheSet(se:muiltiLatonSe,assetNamespace:String) {
        _caches = new Dictionary();
        _assetNamespace = assetNamespace;
    }

    public function getClipCache(assetNamespace:String,clip:SkeletonAnimationClip):SkeletonClipCache
    {
        if(clip in _caches){
            return _caches[clip] as SkeletonClipCache;
        }
        if(_cache_animator.indexOf(assetNamespace)>=0){
            _caches[clip] = new SkeletonClipCache(clip.totalFrame,clip.joints);
            cache_size += clip.totalFrame * clip.joints * (SkinnedShader._JointSkinDataLen + SkinnedShader._JointPoseMatrixLen);
            cache_clips ++;
            //trace("ClipCache: " + _assetNamespace + " cached: " + clip.name + " cache_size:" + (cache_size/(1024.0*1024.0)).toFixed(4) + "MB,cache_clips:"+cache_clips.toString());
            return  _caches[clip] as SkeletonClipCache;
        }
        return null;
    }

    public static function get enable():Boolean {
        return _enable;
    }

    public static function set enable(value:Boolean):void {
        _enable = value;
    }
}
}

internal class muiltiLatonSe{

}
